//生成UUID的方法
function guid() {
  function S4() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
  }
  return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}



function gridList(objold,id){
	var obj = objold;
	var ub = obj.url;
	gridObj[id] = {};
	if(obj.pageNo){
		gridObj[id].pageNo = obj.pageNo;
	}
	gridObj[id].gridobj = obj;
	gridObj[id].gridid = id;
	loading_table(id);
	var w = {pageNumber:obj.pageNo,pageSize:obj.pageSize,tableName:obj.table,orderBy:obj.order};
	if(obj.sql){
		w["sql"] = encodePass(obj.sql);
	}
	//如果有其它参数，也把参数一起复制到当前参数中
	if(obj.parms){
		copy(obj.parms,w);
	}
	copy(obj.where,w);
	
	if(ub.indexOf("FrameAction@list.action")!=-1){
		w.where = encodePass(obj.where.where);
		
	}
	
	//查询数据
	$.ajax({
		url : obj.url,
		data : w,
		type : "POST",
		success : function(text){
			var j = decode(text);
			gridObj[id].pageObject = j;
			obj.total = j.total;
			var col = obj.columns;
			//输出头部工具条按钮 
			var button = getButtonHtml1(obj,Number(getColSum(col)));
			var table = button+'<div class="rightinfo" ><div class="content" id="content_table_id" >';
			var wid = Number($(window).width())+300;
			table = table+'<table class="table_content" >';
			
			//table = table+button;
			table = table+"<tr>";
			//循环输出表头
			for(var a=0;a<col.length;a++){
				var colspan = "";
				if(col[a].colspan){
					colspan = "colspan=\""+col[a].colspan+"\"";
				}
				if(col[a].checkbox){//如果该列是选择框 
					table = table+'<th width="'+col[a].width+'" '+colspan+' ><input type="checkbox"   name="checkAll" title="全选/取消" onclick="f_selectAll(this)" ></th>';
					table = table+'<th width="5%">序号</th>';
				}else{
					if(a == 0){
						table = table+'<th width="5%">序号</th>';
					}
					table = table+'<th width="'+col[a].width+'" '+colspan+'  th="true" >'+col[a].title+'</th>';
				}
				
			}
			table = table+"</tr>";
			//开始输出数据
			var rows = j.rows;
			for(var i=0;i<rows.length;i++){
				//双数行变颜色
				if(i%2 == 0){
					table = table+"<tr c=''  >";
				}else{
					table = table+"<tr class='listShowBag' c='#F5ECEC'  >";
				}
				
				var r = rows[i];
				table_mx.addRowData(r);
				var check = false;
				var tm = "";
				for(var a=0;a<col.length;a++){
					var style = getColumnStyle(col[a]);
					var c = col[a].field;
					if(col[a].formatter){
						if(col[a].colspan){
							var vs = col[a].formatter(r[c],r);
							for(var g=0;g<col[a].colspan;g++){
								tm = tm+'<td width="65px" '+style+' onclick="gridTrClick(this)" >'+vs[g]+'</td>';
							}
						}else{
							var c_id = col[a].field+"_"+i+"_"+a+guid();
							col[a]["td_id_ajax"] = c_id;
							var td_id = "td_hidden_"+c_id;
							tm = tm+'<td '+style+' id="'+td_id+'"  colName="'+c+'" onclick="gridTrClick(this)" >'+col[a].formatter(r[c],r,col[a],td_id)+'</td>';
						}
					}else if(col[a].checkbox){//如果该列是选择框 
						tm = tm+'<td '+style+' ><input type="checkbox"   name="checkbox_check" value="'+r[col[a].id_col]+'"   colName="'+col[a].id_col+'" /></td>';
						tm = tm+'<td '+style+' onclick="gridTrClick(this)" >'+((pageNo-1)*pageSize+1+i)+'</td>';
						check = true;
					}else{
						var td_value = r[c]==null?"":r[c];
						td_value = td_value=="null"?"":td_value;
						tm = tm+'<td '+style+'  colName="'+c+'" onclick="gridTrClick(this)"  >'+td_value+'</td>';
					}
				}
				if(!check){
					table = table+'<td onclick="gridTrClick(this)" >'+((pageNo-1)*pageSize+1+i)+'</td>';
				}
				table = table+tm;
				table = table+"</tr>";
			}
			table = table+'</table>';
			table = table+"</div>";
			table = table+pager(j,id);
			table = table+"</div>";
			gridObj[id].pageCount = j.pageCount;
			$("#"+id).addClass("body_div");
			$("#"+id).html(table);
			$("#"+id+"_select_pager_select").val(obj.pageSize);
			$("#loading").remove();
			$("#"+id).show();
			
			if(window.screen.height == 0){
				$("#"+id).height(document.body.clientHeight-100);
			}
			
			$("[name=checkbox_check]").change(function() { 
				gridTrColor(this);
			});
			
			//$("[th=true]").resizable();
			if(obj.alter_fun){
				obj.alter_fun();
			}
		}
	});
}







function gridList2(obj,id){
	var ub = obj.url;
	gridObj[id] = {};
	if(obj.pageNo){
		gridObj[id].pageNo = obj.pageNo;
	}
	gridObj[id].gridobj = obj;
	gridObj[id].gridid = id;
	loading_table(id);
	var initKey = obj.initKey;
	var keylist = obj.keylist;
	for(var v1=0;v1<initKey.length;v1++){
		keylist.push(initKey[v1]);
	}
	var w = {pageNumber:obj.pageNo,pageSize:obj.pageSize,tableName:obj.table,orderBy:obj.order,keylist:JSON.stringify(keylist)};
	if(obj.sql){
		obj.sql = encodePass(obj.sql);
		w["sql"] = obj.sql;
	}
	//如果有其它参数，也把参数一起复制到当前参数中
	if(obj.parms){
		copy(obj.parms,w);
	}
	copy(obj.where,w);
	
	
	
	//查询数据
	$.ajax({
		url : rootPath+"/FrameAction2/list",
		data : w,
		type : "POST",
		success : function(text){
			var j = decode(text);
			gridObj[id].pageObject = j;
			obj.total = j.total;
			if(!obj.columns){
				obj.columns = j.listColumn;
			}
			
			var col = obj.columns;
			//输出头部工具条按钮 
			var button = getButtonHtml1(obj,Number(getColSum(col)));
			var table = button+'<div class="rightinfo" ><div class="content" id="content_table_id" >';
			var wid = Number($(window).width())+300;
			table = table+'<table class="table_content" >';
			
			//table = table+button;
			table = table+"<tr>";
			//循环输出表头
			for(var a=0;a<col.length;a++){
				var colspan = "";
				if(col[a].colspan){
					colspan = "colspan=\""+col[a].colspan+"\"";
				}
				if(col[a].checkbox){//如果该列是选择框 
					table = table+'<th width="'+col[a].width+'" '+colspan+' ><input type="checkbox"   name="checkAll" title="全选/取消" onclick="f_selectAll(this)" ></th>';
					table = table+'<th width="5%">序号</th>';
				}else{
					if(a == 0){
						table = table+'<th width="5%">序号</th>';
					}
					table = table+'<th width="'+col[a].width+'" '+colspan+'  th="true" >'+col[a].title+'</th>';
				}
				
			}
			table = table+"</tr>";
			//开始输出数据
			var rows = j.rows;
			for(var i=0;i<rows.length;i++){
				//双数行变颜色
				if(i%2 == 0){
					table = table+"<tr c=''  >";
				}else{
					table = table+"<tr class='listShowBag' c='#F5ECEC'  >";
				}
				
				var r = rows[i];
				table_mx.addRowData(r);
				var check = false;
				var tm = "";
				for(var a=0;a<col.length;a++){
					var style = getColumnStyle(col[a]);
					var c = col[a].field;
					if(col[a].formatter){
						if(col[a].colspan){
							var vs = col[a].formatter(r[c],r);
							for(var g=0;g<col[a].colspan;g++){
								tm = tm+'<td width="65px" '+style+' onclick="gridTrClick(this)" >'+vs[g]+'</td>';
							}
						}else{
							var c_id = col[a].field+"_"+i+"_"+a;
							col[a]["td_id_ajax"] = c_id;
							var td_id = "td_hidden_"+c_id;
							tm = tm+'<td '+style+' id="'+td_id+'"  colName="'+c+'" onclick="gridTrClick(this)" >'+col[a].formatter(r[c],r,col[a],td_id)+'</td>';
						}
					}else if(col[a].checkbox){//如果该列是选择框 
						tm = tm+'<td '+style+' ><input type="checkbox"   name="checkbox_check" value="'+r[col[a].id_col]+'"   colName="'+col[a].id_col+'" /></td>';
						tm = tm+'<td '+style+' onclick="gridTrClick(this)" >'+((pageNo-1)*pageSize+1+i)+'</td>';
						check = true;
					}else{
						var td_value = r[c]==null?"":r[c];
						td_value = td_value=="null"?"":td_value;
						tm = tm+'<td '+style+'  colName="'+c+'" onclick="gridTrClick(this)"  >'+td_value+'</td>';
					}
				}
				if(!check){
					table = table+'<td onclick="gridTrClick(this)" >'+((pageNo-1)*pageSize+1+i)+'</td>';
				}
				table = table+tm;
				table = table+"</tr>";
			}
			table = table+'</table>';
			table = table+"</div>";
			table = table+pager2(j,id);
			table = table+"</div>";
			gridObj[id].pageCount = j.pageCount;
			$("#"+id).addClass("body_div");
			$("#"+id).html(table);
			$("#"+id+"_select_pager_select").val(obj.pageSize);
			$("#loading").remove();
			$("#"+id).show();
			
			$("[name=checkbox_check]").change(function() { 
				gridTrColor(this);
			});
			
			//$("[th=true]").resizable();
			if(obj.alter_fun){
				obj.alter_fun();
			}
		}
	});
}





var pager2 = function(page,id){
	var html = '<div class="pages" style="margin-top:0px;" ><div class="dpl-paging" style="margin-top:0px;" >';
	html = html+'<span class="dpl-page-list">';
	html = html+'<a class="dpl-next" onclick=f_per2(this)  grid_id="'+id+'" >上一页</a>  <a class="dpl-next" onclick=f_next2(this)   grid_id="'+id+'" ><span class="dpl-txt" >下一页</span>';
	html = html+'</a>';
	
	var se = "<option value='15' >15</option>";
	se = se+"<option value='30' >30</option>";
	se = se+"<option value='50' >50</option>";
	se = se+"<option value='70' >70</option>";
	se = se+"<option value='100' >100</option>";
	se = se+"<option value='200' >200</option>";
	se = se+"<option value='500' >500</option>";
	html = html+'<select  onchange="f_load_grid2(this)"  grid_id="'+id+'" id="'+id+'_select_pager_select" >'+se+'</select>';
	
	html = html+'</span> <span class="dpl-paging-total">当前第'+page.pageNumber+'/'+page.pageCount+'页, 共'+page.total+'条记录</span> <span';
	html = html+' class="dpl-number"><span> 到</span> <input';
	html = html+' class="dpl-text dpl-text-s" type="text" id="pageText" value="'+page.pageNumber+'" maxlength="6"></input><span>页</span></span> <span';
	
	html = html+' class="dpl-forward"><button class="btn btn-js"';
	html = html+' style="padding:3px 6px;" onclick="f_go2(this)"  grid_id="'+id+'"  >确定</button></span>';
	html = html+'</div></div>';
	
	return html;
};


function f_load_grid2(o){
	var id = $(o).attr("grid_id");
	gridObj[id].pageNo = 1;
	gridObj[id].pageSize = $(o).val();
	gridObj[id].gridobj.pageSize = $(o).val();
	gridList2(gridObj[id].gridobj, id);
}

function f_per2(o){
	var id = $(o).attr("grid_id");
	
	if(gridObj[id].pageNo == 1){
		alert("当前已经是第一页了");
		return false;
	}
	var pageNo = Number(gridObj[id].pageNo);
	pageNo--;
	gridObj[id].gridobj.pageNo = pageNo;
	gridList2(gridObj[id].gridobj, id);
}
function f_next2(o){
	var id = $(o).attr("grid_id");
	if(gridObj[id].pageNo == gridObj[id].pageObject.pageCount){
		alert("当前已经是最后一页了");
		return false;
	}
	var pageNo = Number(gridObj[id].pageNo);
	pageNo++;
	gridObj[id].gridobj.pageNo = pageNo;
	gridList2(gridObj[id].gridobj, id);
}

function f_go2(o){
	var id = $(o).attr("grid_id");
	gridObj[id].gridobj.pageNo = $("#"+id).find("#pageText").val();
	
	if(Number(gridObj[id].pageNo)<1){
		alert("输入页码不能小于等于0");
		return false;
	}
	if(Number(gridObj[id].pageNo) > gridObj[id].pageObject.pageCount){
		alert("当前已经是最后一页了");
		return false;
	}
	
	gridList2(gridObj[id].gridobj, id);
}


/**
 * 对页面加载下拉框
 */
function loadSelectCombox21(id,defult_text){
	if(defult_text == null || defult_text == ""){
		defult_text = "请选择";
	}

	var selects = $("select");
	var listattrs = [];
	for(var a=0;a<selects.length;a++){
		var sqls = {};
		sqls["selectid"] = $(selects[a]).attr("selectid");
		sqls["col_style"] = $(selects[a]).attr("col_style");
		sqls["dict_id"] = $(selects[a]).attr("dict_id");
		sqls["zygl_id"] = $(selects[a]).attr("zygl_id");
		sqls["v"] = $(selects[a]).attr("v");
		sqls["text"] = $(selects[a]).attr("text");
		listattrs.push(sqls);
		
		if(sqls["col_style"]!=null && sqls["col_style"]!=""){
			$(selects[a]).html("<OPTION value='' >正在加载中...</OPTION>");
		}
	}
	$.ajax({
		url : rootPath+"/FrameAction2/loadComboxSelect",
		data : {liststr:JSON.stringify(listattrs)},
		type : "POST",
		success : function(text){
			var l_map = decode(text);
			for(var n_key in l_map){
				var obj_select = $("[selectid="+n_key+"]");
				obj_select = $(obj_select[0]);
				//获取默认值
				var default_value = $(obj_select).attr("default_value");
				$(obj_select).html("<OPTION value='' ></OPTION>");
				var j = l_map[n_key];
				for(var c=0;c<j.length;c++){
					var mapv = j[c];
					var v = mapv.v;
					var name = mapv.text;
					if(default_value){
						if(v == default_value){
							$(obj_select).append("<OPTION value='"+v+"' selected='selected' >"+name+"</OPTION>");
						}else{
							$(obj_select).append("<OPTION value='"+v+"' >"+name+"</OPTION>");
						}
					}else{
						$(obj_select).append("<OPTION value='"+v+"' >"+name+"</OPTION>");
					}
					
				}
			}
			
			
		}
	});

}

























